<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\AdminRole;
use app\admin\model\Admin;

class Root extends AdminBase{

    //当前控制器菜单
 	public $submenu = [];
    
 	/**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '管理员管理', 'url' => url('admin/root/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '管理员添加', 'url' => url('admin/root/add'), 'icon' => '&#xe654;'];
    }

    /**
     * 管理员管理
     * @access public
     * @return \think\Response
     */
    public function index($roleid = 0) {
        $role_id = (int)$roleid;
        $Admin = new Admin;
        if($roleid){
            $Admin->where('role_id',$role_id);
        }
    	$list = $Admin->order('aid','asc')->paginate(config('mn_admin.admin_pagesize'));
    	$this->assign([
            'list' => $list,
            'submenu' => $this->submenu,
        ]);
    	return $this->fetch();
    }

    /**
     * 添加
     * @access public
     * @return \think\Response
     */
    public function add(){
        $Admin = new Admin;
        if($this->request->isPost()){
            $data = $Admin->register($this->request->param('data/a'));
            if(is_numeric($data)){
                $this->success('管理员['.$Admin->username.' : '.$Admin->aid.']添加成功','index');
            }else{
                $this->error($Admin->getError());
            }
        }

        $this->assign([
            'role' => AdminRole::getRoleAll(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 修改
     * @access public
     * @return \think\Response
     */
    public function edit($aid = 0) {
        $aid = (int)$aid;
        if(!$aid){
            $this->error('缺少管理员ID','index');
        }
        $data = Admin::get($aid);
        if(!$data){
            $this->error('此管理员不存在','index');
        }
        if($this->request->isPost()){
            $post_data = $this->request->param('data/a');
            $post_data['role_id'] = (int)$post_data['role_id'] ? (int)$post_data['role_id'] : $this->error('非法的角色组参数');
            foreach ($post_data as $key => $value) {
                if($post_data['role_id'] != 1 && $aid == 1){
                    $this->error('此超级管理员不能修改角色组');
                }
                //去除表单数据为空的字段
                if(empty($value) && $key != 'name'){
                    unset($post_data[$key]);
                }

                //验证重复
                if(!empty($value) && $key == 'phone'){
                    if($data->where('aid','<>',$aid)->where('phone',$value)->find()){
                        $this->error('手机号码已被使用');
                    }
                }
                if(!empty($value) && $key == 'email'){
                    if($data->where('aid','<>',$aid)->where('email',$value)->find()){
                        $this->error('邮箱已被使用');
                    }
                }
            }
            if(false !== $data->allowField(true)->validate('Admin.edit')->save($post_data,['aid'=>$aid])){
                $this->success('修改成功','index');
            }else{
                $this->error($data->getError());
            }
        }
        $this->assign([
            'role' => AdminRole::getRoleAll(),
            'data' => $data->getData(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 删除
     * @access public
     * @return void
     */
    public function del($aid = 0) {
        $aid = (int)$aid;
        if(!$aid){
            $this->error('缺少管理员ID','index');
        }
        if($aid == 1){
            $this->error('超级管理员不允许删除','index');
        }
        $data = Admin::get($aid);
        if(!$data){
            $this->error('需要删除的管理员不存在','index');
        }
        $data->delete();
        $this->success('已成功删除管理员','index');
    }

    /**
     * 登录日志
     * @access public
     * @return \think\Response
     */
    public function log($aid = 0) {
        $aid = (int)$aid;
        if(!$aid){
            $this->error('缺少管理员ID','index');
        }
        $list = Admin::where('aid',$aid)->order('id','desc')->paginate(config('mn_admin.admin_pagesize'));
        $this->assign([
            'list' => $list,
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }
}